package com.anpai.main;

import com.anpai.entity.UserBean;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;

public class registerFrame extends JFrame implements ActionListener {
    private JLabel userIdLa;
    private JLabel pwdLa;
    private JLabel nameLa;
    private JLabel studentNoLa;
    private JLabel majorLa;
    private JLabel classLa;

    private JPasswordField pwdTxt;
    static JTextField nameTxt;
    static JTextField userNameTxt;
    static JTextField studentTxt;
    static JTextField majorTxt;
    static JTextField classTxt;
    private JButton sureBt;
    private JButton quitBt;
    private static String xlsPath;
    public static String path = System.getProperty("user.dir");

    static HashMap<String, UserBean> userMap = new HashMap<>();


    // 构造方法
    public registerFrame()
    {
        this.setTitle("考试系统-用户注册");
        Init();
    }
    public void Init()
    {
        xlsPath = path+"\\src\\main\\java\\com\\anpai\\data\\user.xls";
        // 用户文本
        userIdLa = new JLabel();
        userIdLa.setText("账号：");
        userIdLa.setSize(60, 50);
        userIdLa.setLocation(100, 20);

        // 密码文本
        pwdLa = new JLabel();
        pwdLa.setText("密码：");
        pwdLa.setSize(50, 50);
        pwdLa.setLocation(100, 60);

        nameLa = new JLabel();
        nameLa.setText("姓名：");
        nameLa.setSize(60, 50);
        nameLa.setLocation(100, 100);


        studentNoLa = new JLabel();
        studentNoLa.setText("学号：");
        studentNoLa.setSize(60, 50);
        studentNoLa.setLocation(100, 140);

        majorLa = new JLabel();
        majorLa.setText("专业：");
        majorLa.setSize(60, 50);
        majorLa.setLocation(100, 180);

        classLa = new JLabel();
        classLa.setText("班级：");
        classLa.setSize(60, 50);
        classLa.setLocation(100, 220);
        // 用户输入框
        userNameTxt = new JTextField();
        userNameTxt.setSize(100, 20);
        // this.setSize(width, height)
        userNameTxt.setLocation(170, 35);

        // 密码输入框
        pwdTxt = new JPasswordField();
        pwdTxt.setSize(100, 20);
        pwdTxt.setLocation(170, 75);


        nameTxt = new JTextField();
        nameTxt.setSize(100, 20);
        nameTxt.setLocation(170, 120);

        studentTxt = new JTextField();
        studentTxt.setSize(100, 20);
        studentTxt.setLocation(170, 160);


        majorTxt = new JTextField();
        majorTxt.setSize(100, 20);
        majorTxt.setLocation(170, 200);

        classTxt = new JTextField();
        classTxt.setSize(100, 20);
        classTxt.setLocation(170, 240);

        // 确认按钮
        sureBt = new JButton("注册");
        sureBt.setSize(60, 25);
        sureBt.setLocation(120, 260);


        // 退出按钮
        quitBt = new JButton("返回");
        quitBt.setSize(60, 25);
        quitBt.setLocation(260, 260);




        this.setLayout(null);
        setBounds(550,300,500, 400);
        this.add(userIdLa);
        this.add(pwdLa);
        this.add(nameLa);
        this.add(studentNoLa);
        this.add(majorLa);
        this.add(classLa);
        this.add(userNameTxt);
        this.add(pwdTxt);
        this.add(nameTxt);
        this.add(studentTxt);
        this.add(majorTxt);
        this.add(classTxt);

        this.add(sureBt);
        this.add(quitBt);
         sureBt.addActionListener(this);
         quitBt.addActionListener(this);

        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    @Override
    public void actionPerformed(ActionEvent e) {
        JButton bt = (JButton) e.getSource();
        String text = bt.getText();
        String pwd = pwdTxt.getText().trim();
        String userName = userNameTxt.getText().trim();

        String name = nameTxt.getText().trim();
        String studentNo = studentTxt.getText().trim();
        String major = majorTxt.getText().trim();
        String classRoom = classTxt.getText().trim();
        if (text.equals("注册")){


            try {
                UserBean userBean = new UserBean(userName,pwd,name,studentNo,major,classRoom);
                write(userBean,"0",null);
                JOptionPane.showMessageDialog(sureBt,"注册成功！");
                this.setVisible(false);

            } catch (Exception exception) {
                exception.printStackTrace();
            }

        }else{
            this.setVisible(false);
             new LoginJFrame();
        }

    }
    public static Workbook getWorkbook(String filePath){
        Workbook workbook = null;
        workbook = new HSSFWorkbook();
        return workbook;
    }
    public  void write(UserBean userBean,String result,Integer index) throws Exception {
        xlsPath = path+"\\src\\main\\java\\com\\anpai\\data\\user.xls";
        FileInputStream fs=new FileInputStream(xlsPath);
        POIFSFileSystem ps=new POIFSFileSystem(fs);  //使用POI提供的方法得到excel的信息
        HSSFWorkbook wb=new HSSFWorkbook(ps);
        HSSFSheet sheet=wb.getSheetAt(0);  //获取到工作表，因为一个excel可能有多个工作表
        HSSFRow row=sheet.getRow(0);  //获取第一行（excel中的行默认从0开始，所以这就是为什么，一个excel必须有字段列头），即，字段列头，便于赋值


        FileOutputStream out=new FileOutputStream(xlsPath);
        if (index==null)
        row=sheet.createRow((short)(sheet.getLastRowNum()+1));//在现有行号后追加数据
        else
            row = sheet.createRow(index);
        System.out.println(sheet.getLastRowNum());
        int rows = sheet.getPhysicalNumberOfRows();
        row.createCell(0).setCellValue(rows-1); //设置第一个（从0开始）单元格的数据
        row.createCell(1).setCellValue(userBean.getUserName());
        row.createCell(2).setCellValue(userBean.getPassword());
        row.createCell(3).setCellValue(userBean.getName());
        row.createCell(4).setCellValue(userBean.getStudentNo());
        row.createCell(5).setCellValue(userBean.getMajor());
        row.createCell(6).setCellValue(userBean.getClassRoom());
        row.createCell(7).setCellValue(result);

        out.flush();
        wb.write(out);
        out.close();

    }

}
